package com.code.exam.mapper;

import com.code.exam.dto.ExamPageQueryDTO;
import com.code.exam.entity.Exam;
import com.code.exam.vo.EPageResultVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface ExamMapper {

    /**
     * 创建考试
     * @param exam
     */
    @Insert("insert into exam(id, title, start_time, end_time, status, admin_id) " +
            "values (#{id}, #{title}, #{startTime}, #{endTime}, #{status}, #{adminId})")
    void examcreate(Exam exam);


    /**
     * 根据id查询考试
     * @param examId
     * @return
     */
    @Select("select * from exam where id = #{examId}")
    Exam getById(Long examId);


    @Update("UPDATE exam SET status = #{status} WHERE id = #{id}")
    void updateStatus(@Param("id") Long id, @Param("status") String status);


    /**
     * 查询总记录数
     * @param query
     * @param startTimeFrom
     * @param startTimeTo
     * @return
     */
        Long countByCondition(@Param("query") ExamPageQueryDTO query,
                              @Param("startTimeFrom") LocalDateTime startTimeFrom,
                              @Param("startTimeTo") LocalDateTime startTimeTo);

    /**
     * 查询当前页数据
     * @param query
     * @param startTimeFrom
     * @param startTimeTo
     * @return
     */
    List<EPageResultVO> selectByCondition(@Param("query") ExamPageQueryDTO query,
                                          @Param("startTimeFrom") LocalDateTime startTimeFrom,
                                          @Param("startTimeTo") LocalDateTime startTimeTo);


    /**
     * 获取考试状态
     * @param examId
     * @return
     */
    @Select("select status from exam where id = #{examId}")
    String getStatusById(Long examId);


    /**
     * 添加题目返回数量
     * @param examId
     * @param questionIds
     * @return
     */
    int addQuestions(@Param("examId") Long examId,@Param("questionIds") List questionIds);

    /**
     * 从考试移除题目并返回数量
     * @param examId
     * @param
     * @return
     */
    int removeTi(@Param("examId") Long examId,@Param("questionIds") List questionIds);

    /**
     * 根据考试id获得所有题目
     * @param examId
     * @return
     */
    List getAllQuestions(Long examId);


    /**
     * 根据考试id获得考试题目
     * @param examId
     * @return
     */
    @Select("select title from exam where id = #{examId}")
    String getTitle(Long examId);
}
